import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  const token = ref(localStorage.getItem('token') || '')
  const userRole = ref(localStorage.getItem('userRole') || '')
  const username = ref(localStorage.getItem('username') || '')

  const setUserInfo = (data) => {
    token.value = data.token
    userRole.value = data.role
    username.value = data.username
    
    localStorage.setItem('token', data.token)
    localStorage.setItem('userRole', data.role)
    localStorage.setItem('username', data.username)
  }

  const clearUserInfo = () => {
    token.value = ''
    userRole.value = ''
    username.value = ''
    
    localStorage.removeItem('token')
    localStorage.removeItem('userRole')
    localStorage.removeItem('username')
  }

  return {
    token,
    userRole,
    username,
    setUserInfo,
    clearUserInfo
  }
}) 